/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var indexTR = null;
var oData = null;
var asControllers = [
"../../core/controller/LoginController.php",
"../../core/controller/DataTableController.php",
"../../core/controller/MtoUsuarioController.php",
"../../core/controller/MtoProductoController.php"
];
var asModulos = [ "admin", "supervision", "venta" ];
var iTipoPerfil;
var fnCommon = function(){
    function fnCheckLogin( iMod ) {
//        console.log( "[fnCheckLogin] << ENTER" );
        var oUser;
        if( !fnIsLoged() ){
            if( iMod != MOD_LOGIN ){
                $( "*" ).remove();
                fnLogout();
            }
        } else {
            oUser = fnCommon.fnGetUserLoged();
            if( oUser.iTipoUsuario != iMod ){
                $( "*" ).remove();
                fnRedirec();
            }
        }
//        console.log( "[fnCheckLogin] >> EXIT" );
    }
    function fnGetUserLoged(){
//        console.log( "[fnGetUserLoged] << ENTER" );
        var oUser;
        var oParams = {
            accion : ACC_GET_USER_LOGED
        }
        oUser = fnSendServer(oParams, CONTROLLER_LOGIN, JSON_FORMAT);
        
        oUser = ( typeof oUser.code == "undefined" ) ? oUser : null;

//        console.log( "[fnGetUserLoged] >> EXIT" );
        return oUser
    }
    
    function fnIsLoged(){
//        console.log( "[fnIsLoged] << ENTER" );
        var blLoged = ( fnGetUserLoged() != null );
//        console.log( "blLoged : " + blLoged  );
//        console.log( "[fnIsLoged] >> EXIT" );
        return blLoged;
    }
    function fnLogout(){
//        console.log( "[fnLogout] << ENTER" );
        var iResp;
        var oParams = {
            accion : ACC_LOGOUT
        }
        iResp = fnSendServer( oParams, CONTROLLER_LOGIN );
        if( iResp == CODE_SUCCESS ){
            location.href = "../login/";
        }
//        console.log( "[fnLogout] >> EXIT" );
    }
    function fnRedirec(){
//        console.log( "[fnRedirec] << ENTER" );
//        var sUrl = "../../pages/mod_";
        var sUrl = "../pages/";
        var oUsuario = fnGetUserLoged();
        if ( oUsuario != null ) {
//            sUrl = sUrl + asModulos[ oUsuario.iTipoUsuario - 1 ];
            location.href = sUrl;
        }
//        console.log( "[fnRedirec] >> EXIT" );
    }
    function fnSendServer( oParams, iController, sTypeResponse ){
//        console.log( "[fnSendServer] << ENTER" );
        var oJSONData = null;
        try {
            
            if( typeof sTypeResponse == 'undefined' ){
                sTypeResponse = "";
            }
            
            $.ajax({
                url : asControllers[ iController ],
                data : oParams,
                async: false,
                success:function( data ) {
                    if( sTypeResponse == JSON_FORMAT ){
                        data = JSON.parse( data );
                    }
                    oJSONData = data;
                }
            });
        } catch ( ex ) { 
            oJSONData = null;
        }
//        console.log( "[fnSendServer] >> EXIT" );
        return oJSONData
    }
    function fnShowInfoAlert( sMessage, iTypeMessage ){
        var asImgAlert = [ "success", "warning", "error" ];
        var sUrlImg = "../resources/images/alert_#.png";
        iTypeMessage = ( typeof iTypeMessage == 'undefined' ) ? IMG_ALERT_WARNING : iTypeMessage;
        
        sUrlImg = sUrlImg.replace( "#", asImgAlert[ iTypeMessage ] );
        
        $( "#img_alert" ).attr( "src", sUrlImg );
        $( "#th_msg_alert" ).text( sMessage );
        jAlert.dialog( "open" );
    }
    function fnShowConfirmAlert( sMessage, iTypeMessage ){
        var asImgAlert = [ "success", "warning", "error" ];
        var sUrlImg = "../resources/images/alert_#.png";
        iTypeMessage = ( typeof iTypeMessage == 'undefined' ) ? IMG_ALERT_WARNING : iTypeMessage;
        
        sUrlImg = sUrlImg.replace( "#", asImgAlert[ iTypeMessage ] );
        
        $( "#img_confirm" ).attr( "src", sUrlImg );
        $( "#th_msg_confirm" ).text( sMessage );
        jConfirm.dialog( "open" );
    }
    function fnShowInfoMessage( sMessage, iTypeMessage ,sWhere){
        var asClassesMessage = [ "cls_msg_success", "cls_msg_warning", "cls_msg_error" ];
        
        iTypeMessage = ( typeof iTypeMessage == 'undefined' ) ? CLS_MSG_WARNING : iTypeMessage;
        $( sWhere + " *" ).remove();
        $( sWhere ).addClass( asClassesMessage[ iTypeMessage ] );
        $( sWhere ).text( sMessage );
        $( sWhere ).css( "display" , "");
    }
    return {
        fnCheckLogin    : fnCheckLogin,
        fnGetUserLoged  : fnGetUserLoged,
        fnIsLoged       : fnIsLoged,
        fnLogout        : fnLogout,
        fnRedirec       : fnRedirec,
        fnSendServer    : fnSendServer,
        fnShowInfoAlert : fnShowInfoAlert,
        fnShowConfirmAlert : fnShowConfirmAlert,
        fnShowInfoMessage : fnShowInfoMessage
    }
}();

$(document).ready(function(){
    $( "#btn_logout" ).click( function(){
        fnCommon.fnLogout();
    } ); 
    jAlert = $("#div_alert").dialog({
        modal: true,
        autoOpen:false,
        width:500,
        title:'Mensaje',
        buttons:{
            "OK":function(){
                $(this).dialog("close");
            }
        }
    });
    jConfirm = $("#div_confirm").dialog({
        modal: true,
        autoOpen:false,
        width:500,
        title:'Confirmación',
        buttons:{
            "OK":function(){
                var bean = $( "#hdd_bean_confirm" ).val();
                switch( bean ){
                    case BEANVENTA:
                        alert( "delete venta" );
                        break;
                    case BEANUSUARIOENBDISAB:
                        fnMtoUsuarioModel.fnHabilitarDesabilitarUsuario();
                        break;
                    case BEANUSUARIO:
                        fnMtoUsuarioModel.fnEliminarUsuario();
                        break;
                }
                $(this).dialog("close");
            },
            "CANCEL":function(){
                $( "hdd_id_bean_confirm" ).val( "-1" );
                $(this).dialog("close");
            }
        }
    });
    
});